package primetest.impl.service;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.inject.Inject;

import primetest.dto.TermDto;
import primetest.impl.dao.TermDao;
import primetest.impl.dao.Transaction;
import primetest.impl.service.TermService;

public class TermService implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = -7986517970461662769L;
	/**
	 *
	 */

	private Transaction transaction;
	private TermDao termDao;

	@Inject
	public TermService(Transaction transaction,TermDao termDao){
		this.transaction = transaction;
		this.termDao = termDao;
	}

	public List<TermDto> getTermList() throws IOException {
		try{
			transaction.begin();
			List<TermDto> termList = termDao.getAllList(transaction);
			//transaction.commit();
			return termList;
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}

	public void addTerm(TermDto termDto) throws IOException {
		try{
			transaction.begin();
			termDao.addTerm(transaction, termDto);
			termDao.getAllList(transaction);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}


	public void updateTerm(TermDto termDto) throws IOException {
		try{
			transaction.begin();
			termDao.updateTerm(transaction, termDto);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}



}
